function [ discretized_roads,graph_export,unique_edges, dist_info ] = discretize_roads_speed_brazil( code_control,gridmap,places2,speeds, distances,edges,unique_nodes, cname )

%% preliminaries
% coordinates of grid centroids
Xtemp = cell2mat( {places2.X} )';
Ytemp = cell2mat( {places2.Y} )';

%% vertical-horizontal neighbors

unique_edges = edges.unique_edges;
discretized_dist = zeros( length( unique_edges ),1 );
discretized_avI = zeros( length( unique_edges ),1 );
actual_dist = zeros( length( unique_edges ),1 );
keep = zeros( length( unique_edges ),1 );
paths = cell( length( unique_edges ),1 );

keep_dist = zeros( length( unique_edges ),1 );
keep_distD = zeros( length( unique_edges ),1 );

dist_info = ones(length(unique_edges), 2);

for j=1:length(unique_edges)
    dist_info(j, 1) = keep_dist(j);
    dist_info(j, 2) = keep(j);
end

%% assign infrastructure

% average infrastructure over jk
chi_nat = 1/5.7;
avI_min = 1e-4;

for n =1:length(unique_edges)
    discretized_avI(n) = speeds(n);
    discretized_dist(n) = distances(n);
end
           
% assign a quantile to edge
temp = discretized_avI( discretized_avI>avI_min );
quantiles_discretized_avI = quantile( temp,code_control.N_cat );

% create map structure
for j=1:length( unique_edges )
    
    % coordinates of each link
   discretized_roads(j).Geometry = 'Line';
   discretized_roads(j).X = [ Xtemp( unique_edges(j,1) ) Xtemp( unique_edges(j,2) ) NaN ];
   discretized_roads(j).Y = [ Ytemp( unique_edges(j,1) ) Ytemp( unique_edges(j,2) ) NaN ];
   
   % investment and distance in each link
   discretized_roads(j).avI = discretized_avI(j);
   discretized_roads(j).totdist = discretized_dist(j);
   
   % is there a link?
   discretized_roads(j).keep = keep(j);

   % quantiles of avI of each link
   if discretized_avI(j)>avI_min
        discretized_roads(j).quantiles = sum( discretized_roads(j).avI>quantiles_discretized_avI )+1;  
   else
        discretized_roads(j).quantiles = 0.1;
   end
   
end


%% build structure to export to the solver

graph_export.J = length(places2);

x = zeros(graph_export.J,1);
y = zeros(graph_export.J,1);
for j=1:length(places2)
    
    graph_export.nodes{j}.neighbors = places2(j).neighbors; 
    graph_export.nodes{j}.x = places2(j).X;
    graph_export.nodes{j}.y = places2(j).Y;
    
    x(j) = places2(j).X;
    y(j) = places2(j).Y;
    
end

% vectors with coordinates of each location
graph_export.x=x;
graph_export.y=y;

% graph with discretized network
EdgeTable = table( unique_edges,discretized_avI,...
                   discretized_dist,...
                   'VariableNames',...
                   {'EndNodes','Weight',...
                   'Distance'} );   % 'Weight' is the average investment         
grid_graph = graph( EdgeTable );

% adjacency matrix - defined based on avI (variable 'Weight' defined above)
graph_export.adjacency = full( adjacency( grid_graph ) );    

% export some matrixes
nn = numnodes( grid_graph );
[s,t]=findedge( grid_graph );

    % matrix of avI
    avI_mat = full( sparse( s,t,grid_graph.Edges.Weight,nn,nn ) );  % this gives an upper triangular matrix
    avI_mat = avI_mat+avI_mat.' - diag( diag( avI_mat ) );          % this filles the lower-triangular part
    graph_export.avI = avI_mat;

    % matrix of distances
    distance_mat = full( sparse( s,t,grid_graph.Edges.Distance,nn,nn ) );       % this gives an upper triangular matrix
    distance_mat = distance_mat+distance_mat.' - diag( diag( distance_mat ) );  % this filles the lower-triangular part
    graph_export.distance = distance_mat;

%% add matrix with bilateral distances    

J = graph_export.J;
all_distances = zeros( J,J );
Xtemp = cell2mat({places2.X});
Ytemp = cell2mat({places2.Y});

for i=1:J
        all_distances( i,: ) = deg2km( distance( ones( 1,J )*Ytemp(i),ones( 1,J )*Xtemp(i),...
                                                 Ytemp,Xtemp ) );
end

% export keep_distance
graph_export.keep_dist = keep_dist;
graph_export.keep_distD = keep_distD;

% distances
graph_export.all_distances = all_distances;
    
% population 
graph_export.L = cell2mat({places2.population})';

% income
for i = 1:length(places2)
    places2(i).income = cast(places2(i).income,'double');
end
    
graph_export.Y = cell2mat({places2.income})';

% geography
graph_export.av_alt = cell2mat({places2.av_alt})';
graph_export.sd_alt = cell2mat({places2.sd_alt})';
graph_export.rugged = cell2mat({places2.rugged})';

                    
                   

